package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

import java.io.File;
import java.util.concurrent.TimeUnit;

public class SeleniumUtils {

    /**
     * 构造WebDriver
     *
     * @return WebDriver
     */
    public static WebDriver getDriverInstance() {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("–-no-sandbox");
        chromeOptions.addArguments("–-disable-dev-shm-usage");
        chromeOptions.addArguments("--disable-gpu");

        String absolutePath = new File("").getAbsolutePath();
        chromeOptions.addArguments("--user-data-dir=" + absolutePath + "/.cache");

        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.manage().timeouts().implicitlyWait(Settings.DELAY_TIME, TimeUnit.SECONDS);
        return driver;
    }

    /**
     * 模拟登录
     *
     * @param driver   driver
     * @param userName 用户名
     * @param password 密码
     */
    public static void login(WebDriver driver, String userName, String password) {
        driver.get(Settings.URL);
        driver.findElement(By.cssSelector(".el-form-item:nth-child(1) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-form-item:nth-child(1) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-form-item:nth-child(1) .el-input__inner")).sendKeys(userName);
        driver.findElement(By.cssSelector(".el-form-item:nth-child(2) .el-input__inner")).sendKeys(password);
        driver.findElement(By.cssSelector(".el-form-item:nth-child(2) .el-input__inner")).sendKeys(Keys.ENTER);
    }

    /**
     * 模拟修改密码
     *
     * @param driver    driver
     * @param oldPwd    原密码
     * @param newPwd    新密码
     * @param repeatPwd 重复新密码
     */
    public static void changePassword(WebDriver driver, String oldPwd, String newPwd, String repeatPwd) {
        {
            WebElement element = driver.findElement(By.cssSelector(".menuActionItem > span:nth-child(1)"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        driver.findElement(By.cssSelector(".menuActionItem > span:nth-child(1)")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(1) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(1) .el-input__inner")).sendKeys(oldPwd);
        driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).sendKeys(newPwd);
        driver.findElement(By.cssSelector(".el-row:nth-child(3) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(3) .el-input__inner")).sendKeys(repeatPwd);
        driver.findElement(By.cssSelector(".confirm > span")).click();
    }

    /**
     * 模拟登出
     *
     * @param driver driver
     */
    public static void logout(WebDriver driver) {
        {
            WebElement element = driver.findElement(By.cssSelector(".menuActionItem > span:nth-child(2)"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        driver.findElement(By.cssSelector(".menuActionItem > span:nth-child(2)")).click();
    }
}
